//
//  Login_ViewController.m
//  International_Hospital
//
//  Created by LiYiLin on 16/7/4.
//  Copyright © 2016年 LiYiLin. All rights reserved.
//

#import "Login_ViewController.h"
#import "Left_ViewController.h"
#import "Home_ViewController.h"

@interface Login_ViewController ()

//顶部图片
@property (nonatomic, strong) UIImageView * hospital_UP_IMV;
//底部图片
@property (nonatomic, strong) UIImageView * hospital_Down_IMV;
//账号
@property (nonatomic, strong) UITextField * account_TF;
//密码
@property (nonatomic, strong) UITextField * password_TF;
//登录按钮
@property (nonatomic, strong) UIButton    * login_Btn;



@end


@implementation Login_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setup_UI];\

}

#pragma mark - 配置UI
- (void)setup_UI {
    
    [self setTitle:@"登录"];
    [self.view addSubview:self.hospital_UP_IMV];
    [self.view addSubview:self.hospital_Down_IMV];
    [self.view addSubview:self.account_TF];
    [self.view addSubview:self.password_TF];
    [self.view addSubview:self.login_Btn];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    
    CGFloat padding = (SCREEN_WIDTH == 320) ? 10 : 70;
    
    //设置顶部图片
    [self.hospital_UP_IMV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(64 + padding);
        make.centerX.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(90, 110));
    }];
    
    //设置底部图片
    [self.hospital_Down_IMV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.hospital_UP_IMV.mas_bottom).offset(2);
        make.centerX.equalTo (self.hospital_UP_IMV);
        make.size.mas_equalTo(CGSizeMake(90, 50));
        
    }];
    
    //设置account_TF
    [self.account_TF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.hospital_Down_IMV.mas_bottom).offset(20);
//        make.center.equalTo(self.view);
        make.height.mas_equalTo(30);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
    }];
    
    //设置password_TF
    [self.password_TF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.account_TF.mas_bottom).offset(20);
//        make.center.equalTo(self.view);
        make.height.mas_equalTo(30);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
    }];
    
    //设置loginButton
    [self.login_Btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.password_TF.mas_bottom).offset(30);
//        make.center.equalTo(self.view);
        make.height.mas_equalTo(40);
        make.left.equalTo(self.view).offset(20);
        make.right.equalTo(self.view).offset(-20);
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.account_TF endEditing:YES];
    [self.password_TF endEditing:YES];
}

#pragma mark - 控件懒加载
//上部图片
- (UIImageView *)hospital_UP_IMV {
    if (!_hospital_UP_IMV) {
        _hospital_UP_IMV = [[UIImageView alloc] init];
        _hospital_UP_IMV.image = [UIImage imageNamed:@"product_img_logo_nor.png"];
    }
    return _hospital_UP_IMV;
}
//下部图片
- (UIImageView *)hospital_Down_IMV {
    if (!_hospital_Down_IMV) {
        _hospital_Down_IMV = [[UIImageView alloc] init];
        _hospital_Down_IMV.image = [UIImage imageNamed:@"product_text_logo_nor.png"];
    }
    return _hospital_Down_IMV;
}
//账号
- (UITextField *)account_TF {
    if (!_account_TF) {
        _account_TF = [[UITextField alloc] init];
        _account_TF.placeholder = @"请输入账号";
        _account_TF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _account_TF.autocapitalizationType = UITextAutocapitalizationTypeNone;
        _account_TF.borderStyle = UITextBorderStyleRoundedRect;
    }
    return _account_TF;
}
//密码
- (UITextField *)password_TF {
    if (!_password_TF) {
        _password_TF = [[UITextField alloc] init];
        _password_TF.placeholder = @"请输入密码";
        _password_TF.clearButtonMode = UITextFieldViewModeWhileEditing;
        _password_TF.autocapitalizationType = UITextAutocapitalizationTypeNone;
        _password_TF.borderStyle = UITextBorderStyleRoundedRect;
    }
    return _password_TF;
}
//登录按钮
- (UIButton *)login_Btn {
    if (!_login_Btn) {
        _login_Btn = [[UIButton alloc] init];
        [_login_Btn setTitle:@"登录" forState:UIControlStateNormal];
        _login_Btn.backgroundColor = kThemeColor;
        _login_Btn.titleLabel.font = FONT(20);
        [_login_Btn addTarget:self action:@selector(clickLogin_Btn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _login_Btn;
}


- (void)clickLogin_Btn:(UIButton *)button{
    
    if ([self.account_TF.text isEqualToString:@"aa"] && [self.password_TF.text isEqualToString:@"aa"]) {
        
        UIAlertController * alertC = [UIAlertController alertControllerWithTitle:@"登录成功!" message:@"健康生活每一天!" preferredStyle:UIAlertControllerStyleAlert ];
        UIAlertAction * action = [UIAlertAction actionWithTitle:@"开始" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            self.loginOrNo = YES;
            
            NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
            
            [defaults setBool:YES forKey:@"loginOrNo"];
            
            [defaults synchronize];
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }];
        [alertC addAction:action];
        [self presentViewController:alertC animated:YES completion:nil];
        
        
    } else if ([self.account_TF.text isEqualToString:@""] || [self.password_TF.text isEqualToString:@""]) {
        
        UIAlertController * alertC = [UIAlertController alertControllerWithTitle:@"请输入账号和密码!" message:@"健康生活每一天!" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction * action = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDestructive handler:nil];
        
        [alertC addAction:action];
        [self presentViewController:alertC animated:YES completion:nil];
        
    } else {
        
        UIAlertController * alertC = [UIAlertController alertControllerWithTitle:@"账号或密码错误!" message:@"健康生活每一天!" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction * action = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleDestructive handler:nil];
        
        [alertC addAction:action];
        [self presentViewController:alertC animated:YES completion:nil];
    }
   
}

@end


















